<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增轮播图')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-banner-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">图片：</label>
                <div class="col-sm-8">
                    <input id="file" accept="image/jpg,image/jpeg,image/png" name="file"  class="form-control" required type="file">
                    <img style="width: 80px ;height: auto;" src="" id="imgPhoto"  />
                    <input style="display: none" id="image" name="image" class="form-control"   type="text">
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">图片标题：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="title" class="form-control" type="text">-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">活动介绍：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input type="hidden" class="form-control" name="title">-->
<!--                    <div class="summernote" id="title"></div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group">    
                <label class="col-sm-3 control-label">是否显示：</label>
                <div class="col-sm-8">
                    <select id="isDel" name="isDel" class="form-control m-b">
                        <option value="1">否</option>
                        <option value="0">是</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">跳转地址：</label>
                <div class="col-sm-8">
                    <input name="url" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">排序，越小越靠前：</label>
                <div class="col-sm-8">
                    <input name="sort" required class="form-control" type="text">
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:src="@{/js/bootstrap-prettyfile.js}"></script>
    <th:block th:include="include :: summernote-js"/>
    <script th:inline="javascript">

        var prefix = ctx + "jiajia/banner"
        $("#form-banner-add").validate({
            focusCleanup: true
        });

        $("#file").bind('change', function () {
            $("#imgPhoto").show();
            $("#imgPhoto").attr("src",URL.createObjectURL($(this)[0].files[0]));

        });

        function submitHandler() {
            if ($.validate.form()) {

                    var formData = new FormData();
                    formData.append('file',$(file)[0].files[0]);
                    $.modal.loading("图片上传中，请稍后...")
                    $.ajax({
                        url: "/common/admin/upload/up/single",
                        data: formData,
                        type: "post",
                        dataType: "json",
                        processData: false,
                        contentType: false,
                        success: function(result) {
                            $("#image").val(result.url);
                            $.operate.save(prefix + "/add", $('#form-banner-add').serialize());
                        }
                    })
                }


        }



        $("input[name='addtime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function () {
            $('.summernote').each(function (i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function (contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function (files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function (result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function (error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });
    </script>
</body>
</html>